package com.lmonkey.servlet.comment;

import com.lmonkey.servic.COMMENTDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/admin/admin_docommentdel")
public class DoCommentDel extends HttpServlet {
    //批量删除
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //获取数据
        String id[] = request.getParameterValues("id[]");
        //进行批量删除
        for (int i=0;i<id.length;i++){
            COMMENTDao.delByid(id[i]);
        }
        //跳转页面
        response.sendRedirect("/lyq/admin/admin_commentselect");
    }
    //单条删除
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //获取参数
        String id = request.getParameter("id");
        //进行删除
       int count = COMMENTDao.delByid(id);
        if (count>0){
            response.sendRedirect("/lyq/admin/admin_commentselect?cp="+request.getParameter("cpage")+"&keywords="+request.getParameter("keywords"));
        }else{
            PrintWriter out = response.getWriter();
            out.println("<script>alert('用户删除失败')location.href='admin_commentselect?cp="+request.getParameter("cpage")+"&keywords="+request.getParameter("keywords")+"'</script>");
        }
    }
}
